<?php
namespace app\api\controller\vppz;


use think\Cache;


use \app\admin\model\vppz\Kjyl as KjylModel;
use \app\admin\model\vppz\KeshiHospital as KeshiHospitalModel;
use \app\admin\model\vppz\CaseBl as CaseBlModel;

use \addons\vppz\library\Vpower;

class Kjyl extends App
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->KjylModel = new KjylModel;
        $this->KeshiHospitalModel = new KeshiHospitalModel;
        $this->CaseBlModel = new CaseBlModel;

    }
    /**
     * 提交病例
     * @return void
     */
    public function uploadcase(){
		$mine=$this->_mine;
			$form = input();
			if(empty($form)){
				$this->error("内容填写有误，请重试");
			}
            // 参数验证
			if(empty($form['contactName']) || empty($form['contactPhone'])){
				$this->error("请填写联系人信息");
			}

			if(empty($form['hospital_name']) || empty($form['department_name'])){
				$this->error("请填写就诊医院信息");
			}
			if(empty($form['image']) || empty($form['image'][0])){
				$this->error("请上传病例信息");
			}
			if(empty($form['file']) || empty($form['file'][0])){
				$this->error("请上传附件信息");
			}
            $ksid = 0;
            $KeshiInfo = $this->KeshiHospitalModel->where('id','=',$form['selectKeshi'])->find();
            $data=array(
					'hospital_id'=>$form['selectHospital'],
                    'keshi_id'=>$KeshiInfo['id']??0,
                    'username'=>$form['contactName'],
                    'mobile'=>$form['contactPhone'],
                    'content'=>$form['description'],
                    'images'=>$form['image'],
                    'files'=>$form['file'],
                    'user_id'=>$mine['id']
				);
				$ret=$this->CaseBlModel->save($data);
				$data['id'] = $this->CaseBlModel->id;
				if(empty($ret) || empty($data['id'])){
					$this->error('服务器刚刚走神了，重试看看呢');
				}
				$this->success('病例提交成功');
    }
    public function caselist(){
        $mine=$this->_mine;
        $case = $this->CaseBlModel->where('user_id','=',$mine['id'])->order('createtime','DESC')->select();
        if(empty($case) === FALSE){
            foreach ($case as $bk=>$bj){
                $case[$bk]['createtime'] = date('Y-m-d H:i:s',$case[$bk]['createtime']);
                $bj['images'] = html_entity_decode($bj['images']);
                $array = json_decode($bj['images']);
                if(empty($array) === FALSE){
                    foreach ($array as $k=>$v){
                        $array[$k] = \addons\vppz\library\Vpower::dourl($v);
                    }
                }
                $case[$bk]['images'] = $array;
                $bj['files'] = html_entity_decode($bj['files']);
                $array = json_decode($bj['files']);
                if(empty($array) === FALSE){
                    foreach ($array as $k=>$v){
                        $array[$k] = \addons\vppz\library\Vpower::dourl($v);
                    }
                }
                $case[$bk]['files'] = $array;
            }
        }
		$this->success('',array(
			'now'=>time(),
			'case'=>$case
		));
    }

    /**病例详情
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function casedetail(){
        $mine=$this->_mine;
        $id = input('id','');
        if(empty($id)){
            $this->error('不存在或暂停提供');
        }
        $case = $this->CaseBlModel->where('id','=',$id)->find();
        if(empty($case) === FALSE){
            $case['createtime'] = date('Y-m-d H:i:s',$case['createtime']);
                $case['images'] = html_entity_decode($case['images']);
                $array = json_decode($case['images']);
                if(empty($array) === FALSE){
                    foreach ($array as $k=>$v){
                        $array[$k] = \addons\vppz\library\Vpower::dourl($v);
                    }
                }
                $case['images'] = $array;
                $case['files'] = html_entity_decode($case['files']);
                $array = json_decode($case['files']);
                if(empty($array) === FALSE){
                    foreach ($array as $k=>$v){
                        $array[$k] = \addons\vppz\library\Vpower::dourl($v);
                    }
                }
                $case['files'] = $array;
        }
		$this->success('',array(
			'now'=>time(),
			'case'=>$case
		));
    }
}
